import os
from typing import Union, Optional, Dict, List
from functools import lru_cache
from pydantic import AnyHttpUrl, BaseSettings, IPvAnyAddress, BaseModel
from enum import Enum


class NoticeClassify(str, Enum):
    # 发送企业微信群消息
    bot = 'bot'
    # 发送卡片信息
    card = 'card'


class NoticeName(str, Enum):
    # 阿里云生产日志
    AliyunProdLog = 'AliyunProdLog'
    # 阿里云测试日志
    AliyunItgLog = 'AliyunItgLog'
    # 阿里云生产prometheus
    AliyunProdProm = 'AliyunProdProm'
    # 阿里云生产N9e
    AliyunProdN9e = 'AliyunProdN9e'
    # 阿里云测试N9e
    AliyunItgN9e = 'AliyunItgN9e'
    # 华为云生产日志
    HuaweiProdLog = 'HuaweiProdLog'
    # 华为云生产N9e
    HuaweiProdN9e = 'HuaweiProdN9e'
    # 嘉纷生产日志
    IfastProdLog = 'IfastProdLog'
    # 嘉纷生产N9e
    IfastProdN9e = 'IfastProdN9e'


class NoticeGroupInfo(BaseModel):
    classify: NoticeClassify
    group_id: str = ""


class NoticeN9eGroup(BaseModel):
    n9e: List[NoticeGroupInfo]


class NoticeLogGroup(BaseModel):
    custom: List[NoticeGroupInfo]
    application: List[NoticeGroupInfo]
    bigdata: List[NoticeGroupInfo]


class Settings(BaseSettings):
    # TAGS_METADATA = [
    #     {
    #         "name": "运维管理平台",
    #         "description": "后台API接口",
    #     }
    # ]
    TAGS_METADATA: list[dict] = None
    # 文档地址
    DOCS_URL: str = "/ops/api/docs"
    # 文档关联请求数据接口
    OPENAPI_URL: str = "/ops/api/openapi.json"
    # 禁用 redoc 文档
    REDOC_URL: Optional[str] = None

    # Jwt环境
    ACCESS_TOKEN_EXPIRE_MINUTES: int = 60 * 24 * 7
    SECRET_KEY: str = ''
    ALGORITHM = "HS256"
    REDIS_URL = ""

    # Mysql环境
    MYSQL_USERNAME: str = ""
    MYSQL_PASSWORD: str = ""
    MYSQL_HOST: Union[AnyHttpUrl, IPvAnyAddress] = "0.0.0.0"
    MYSQL_PORT: str = ""
    MYSQL_DATABASE: str = 'ops'
    SQLALCHEMY_DATABASE_URI = f"mysql+pymysql://{MYSQL_USERNAME}:{MYSQL_PASSWORD}@" \
                              f"{MYSQL_HOST}:{MYSQL_PORT}/{MYSQL_DATABASE}?charset=utf8mb4"
    # print(SQLALCHEMY_DATABASE_URI)
    POOL_SIZE: int = 300
    POOL_TIMEOUT: int = 10
    POOL_RECYCLE: int = 7200
    POOL_PRE_PING: bool = True

    # Elasticsearch
    ELASTIC_SCHEME: str = "http"
    ELASTIC_HOST: list = ["127.0.0.1"]
    ELASTIC_PORT: int = 9200
    ELASTIC_USERNAME: Union[str, None] = None
    ELASTIC_PASSWORD: Union[str, None] = None

    # Celery环境
    CELERY_BROKER_URL: str = ""  # NEW
    CELERY_RESULT_BACKEND: str = ""  # NEW

    # consul服务器
    CONSUL_URL: str = ""
    CONSUL_TOKEN: str = ""

    # K8S
    K8S_API: str = ""
    K8S_TOKEN: str = ""
    K8S_CA_PATH: str = ""
    K8S_QOS_NAMESPACE: str = ""
    K8S_PROM_NAMESPACE: str = "monitoring"
    K8S_PROM_URL: str = ""

    # 短连接地址
    SHORT_URL: str = ""

    # 审计配置
    AUDIT_QYWX_GROUP_ID: str = ""

    # OA平台
    OA_API: str = ""
    OA_USERNAME: str = ""
    OA_PASSWORD: str = ""

    # SA平台
    SA_API: str = ""
    SA_TOKEN: str = ""
    SA_CHAT_GROUP_ID: str = ""

    # wbot平台
    WBOT_API: str = ""
    WBOT_CHAT_BOT_ID: str = ""

    # 通知中心配置
    NOTICE_CENTER_N9E_NAME: NoticeName = NoticeName.AliyunProdN9e
    NOTICE_CENTER_N9E_GROUP: NoticeN9eGroup = None
    NOTICE_CENTER_LOG_NAME: NoticeName = NoticeName.AliyunProdLog
    NOTICE_CENTER_LOG_GROUP: NoticeLogGroup = None
    NOTICE_CENTER_LOG_SERVER_TYPE: str = "application"
    NOTICE_CENTER_LOG_LEVEL: str = "warning"
    NOTICE_CENTER_LOG_TEAM: str = "生产运维组"
    NOTICE_CENTER_LOG_DEPARTMENT: str = "运维"

    # ALIYUN SA@welab-inc.com ACCOUNT AK
    SA_ACCOUNT: str = ''
    SA_ACCESS_KEY: str = ''
    SA_ACCESS_SECRET: str = ''

    # ALIYUN tianmian ACCOUNT AK
    TIANMIAN_ACCOUNT: str = ''
    TIANMIAN_ACCESS_KEY: str = ''
    TIANMIAN_ACCESS_SECRET: str = ''

    # ALIYUN welabfintech ACCOUNT AK
    WELABFINTECH_ACCOUNT: str = ''
    WELABFINTECH_ACCESS_KEY: str = ''
    WELABFINTECH_ACCESS_SECRET: str = ''

    # ALIYUN welabconsfin ACCOUNT AK
    WELABCONSFIN_ACCOUNT: str = ''
    WELABCONSFIN_ACCESS_KEY: str = ''
    WELABCONSFIN_ACCESS_SECRET: str = ''

    # HUAWEIYUN tianmiantech ACCOUNT AK
    TIANMIANTECH_ACCOUNT: str = ''
    TIANMIANTECH_ACCESS_KEY: str = ''
    TIANMIANTECH_ACCESS_SECRET: str = ''

    # HUAWEIYUN welabcloud ACCOUNT AK
    WELABCLOUD_ACCOUNT: str = ''
    WELABCLOUD_ACCESS_KEY: str = ''
    WELABCLOUD_ACCESS_SECRET: str = ''

    # 通知邮箱
    EMAIL_HOST: str = ''
    EMAIL_PORT: int = 465
    EMAIL_USERNAME: str = ''
    EMAIL_PASSWORD: str = ''

    APOLLO_DB_HOST: str = ''
    APOLLO_DB_USER: str = ''
    APOLLO_DB_PASSWORD: str = ''
    APOLLO_DB_NAME: str = ''

    FSR_RABBITMQ_QUEUE_URL: str = ''
    FSR_RABBITMQ_QUEUE_TOKEN: str = ''
    FSR_CONTAINER_CPU_URL: str = ''
    FSR_CONTAINER_CPU_TOKEN: str = ''
    FSR_POD_RESTART_TOKEN: str = ''
    FSR_NODE_MEM_TOKEN: str = ''
    FSR_CASSANDRA_RECOVERY_URL: str = ''
    FSR_CASSANDRA_RECOVERY_TOKEN: str = ''

    # jumpserver
    JUMPSERVER_HOST: str = '127.0.0.1'
    JUMPSERVER_USERNAME: str = ''
    JUMPSERVER_PASSWORD: str = ''

    # 大数据
    CM_HOST: str = "127.0.0.1"
    CM_PORT: str = "7180"
    CM_USERNAME: str = ''
    CM_PASSWORD: str = ''

    KUDU_MASTER_HOST: str = "127.0.0.1"
    KUDU_MASTER_PORT: str = "8051"

    HDFS_NAMENODE_WEB_HOST = "127.0.0.1,192.168.100.2"
    HDFS_NAMENODE_WEB_PORT = "9870"

    OPS_HTTP_URL: str = "http://127.0.0.1:8080"
    TEMPLATE_ROOT: str = os.path.join(os.path.dirname(os.path.dirname(os.path.abspath(__file__))), 'templates')

    LDAP_HOST: str = "127.0.0.1"
    LDAP_PORT: int = 389
    LDAP_USER: str = 'ldap'
    LDAP_AUTH: str = 'admin'
    LDAP_PROTOCOL: str = '123456'
    LDAP_BASE_DN: str = 'dc=test,dc=com'
    LDAP_USER_DN: str = 'ou=users,dc=test,dc=com'
    LDAP_GROUP_DN: str = 'ou=groups,dc=test,dc=com'
    
    DORIS_HOST: str = ''
    DORIS_PORT: int = 9030
    DORIS_USERNAME: str = ''
    DORIS_PASSWORD: str = ''
    DORIS_DATABASE: str = ''
    DORIS_ALARM_TABLE: str = ''
    DORIS_HTTP_TABLE: str = ''
    DORIS_LOG_URL: str = ''
    

    class Config:
        # 设置需要识别的 .env 文件
        app_root = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
        env_file = os.path.join(app_root, '.env')

        # 设置字符编码
        env_file_encoding = 'utf-8'

        # env_nested_delimiter = ':'


@lru_cache()
def get_settings():
    return Settings()


config = get_settings()
